from os.path import join, dirname
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette, QBrush, QPixmap
from PyQt5.QtWidgets import QWidget, QApplication

def set_widget_background_full(widget: QWidget, img_path: str):
    palette = QPalette()
    palette.setBrush(widget.backgroundRole(), QBrush(
        QPixmap(img_path).scaled(
            widget.size(),
            Qt.IgnoreAspectRatio,
            Qt.SmoothTransformation
        )))
    widget.setPalette(palette)

class MyWidget(QWidget):
    def __init__(self):
        super(MyWidget, self).__init__()
        self.setWindowTitle("窗口背景设置示例2")
        self.back_img = join(dirname(__file__), "res", "test.jpg")
        set_widget_background_full(self, self.back_img)

    def resizeEvent(self, a0):
        set_widget_background_full(self, self.back_img)
        return super(MyWidget, self).resizeEvent(a0)

app = QApplication([])
w = MyWidget()
w.show()
app.exec_()
